import flash.filters.DropShadowFilter;
import flash.geom.Rectangle;
import com.troyworks.framework.ui.layout.CollisionDetection;
/**
 * The script to do a mouse replacement,
 * can be used with Object.egisterClass or can be
 *  passed in a linkage ID and will create itself on the root timeline
 *  based on the static field DEPTH.
 *  
 * 
 //Object.registerClass("GlovedHandCursor", com.troyworks.ui.CustomCursor);

new com.troyworks.ui.CustomCursor("GlovedHandCursor");
stop();

 *  
 * @author Troy Gardner
 */
class com.troyworks.ui.CustomCursor extends MovieClip {
	public static var DEPTH : Number = 16111;
	public static var ageOfUser : Number = 5;
	public static var rightHanded : Boolean = true;
	public static var isToggle : Boolean = true;
	public static var DEBUG:Boolean = false;
	public static var XRAY : Boolean = false;
	public static var USE_DROPSHADOW : Boolean = true;
	public var isToggled : Boolean = false;
	public var isDown : Boolean = false;
	private var _owidth : Number;
	private var _oheight : Number;
	private var rotRate : Number = 100;
	public static var hittests : Array = new Array();
	public static var instance : CustomCursor;
	public var drawin : MovieClip;
	public var overArray : Array = new Array();
	public var activeArray : Array = new Array();

	private var animate : Boolean = true;
	public var handmode : String = "open";
	private var m_collisionCheckOn:Boolean = true;

	private var lockUI : Boolean = false;
	public var lockUIAfterLoad:Boolean = false;
	private function CustomCursor(linkage : String, aAgeOfUser : Number, aToggle : Boolean){
		trace("new CustomCursor " + this._name);
		if(linkage == null && this._name != null){
			//on stage already
		}else{
			//attach linkage. 
			if(linkage  == null){
				linkage = "cursor";
			}
		}
	}
	public static function getInstance(linkage : String, aAgeOfUser : Number, aToggle : Boolean):CustomCursor{
		Object.registerClass(linkage, com.troyworks.ui.CustomCursor);
		if(CustomCursor.instance == null){
			CustomCursor.DEPTH =  _root.getNextHighestDepth();
			var cc:CustomCursor = 	CustomCursor(_root.attachMovie(linkage, linkage +"_mc", CustomCursor.DEPTH, {_x:_root._xmouse, _y:_root._ymouse}));
			cc.drawin = CollisionDetection.getDebugMC();
			CustomCursor.instance = cc;
		}
		return CustomCursor.instance;
	}
	public function onLoad() : Void{
		trace("ERROR CustomCursor onload " + this.handmode + " " + lockUI);

		Mouse.hide();
		CustomCursor.instance = this;
		//Mouse.addListener(this);
		stop();

	//	_width = _height = 100-( ageOfUser * 10) + 12;
	//	_owidth = _width;
	//	_oheight = _height;
		//_alpha = 30;
		if(USE_DROPSHADOW){
			var drop : DropShadowFilter = new DropShadowFilter(10, 45, 0x000000, .6,5,5, .6);
			this.filters = [drop];	
		}
		updateUI(this.handmode);
		if(lockUIAfterLoad){
			lockUI = true;
		}
	}
	public function set collisionCheckOn(val:Boolean):Void{
			m_collisionCheckOn = val;
			if(m_collisionCheckOn){
				onMouseMove();
			}
		}
		public function get collisionCheckOn():Boolean{
			return m_collisionCheckOn;
		}
	public function makeHandFingerPoint(lock:Boolean) : Void{
		if(lock != null){
			lockUI = lock;
		}
		this.handmode = "point";
		updateUI("point");
		trace("CustomCursor makeHandFingerPoint " + this.handmode);
	}
	public function makeHandOpenPalm(lock:Boolean) : Void{
		if(lock != null){
			lockUI = lock;
		}
		this.handmode = "open";
		updateUI("open");
		isToggled = false;
	}
	public function makeHandPinch(lock:Boolean) : Void{
		if(lock != null){
			lockUI = lock;
		}
		this.handmode = "pinch";
		updateUI("pinch");
	}
	public function addToHitTest(o : Object) : Void{
		hittests.push(o);
	}
	public function clearSelected():Void{
		for (var i : Number = 0; i < activeArray.length; i++) {
			var _mc:MovieClip = MovieClip(activeArray.pop());
			_mc.onRollOut();
			_mc.cursorIsOver = false;
			
		}
	}
	public function onMouseMove(evt : Object) : Void{

		//trace("onMouseMove");
		this._x = _root._xmouse;
		this._y = _root._ymouse;
		if(animate){
			var px : Number = _root._xmouse/(Stage.width*.9);
		//trace("px " + px);
			if(true){
				if(!isDown){
					if(rotRate < 100){
						rotRate+=2;
					}
					this._rotation = (px * rotRate) - 20;
				}else{
					if(rotRate > 40){
						rotRate-=2;
					}
		
					this._rotation = (px * rotRate) - 20;
				}
			}
		}
		if(collisionCheckOn){	
		drawin.clear();
		var hit:Boolean = false;
		var len = hittests.length;
		while(len--){
			var target_mc = hittests[len];
			if(target_mc.customCursorCollisionCheckOn == null || target_mc.customCursorCollisionCheckOn){
			var collisionRect : Rectangle = CollisionDetection.checkForCollision(this,target_mc,10);
		//	trace("performED rollOver ShapeOnShape hittest " + target_mc._name + " " + collisionRect);
			if (collisionRect) {
				hit = true;
					updateUI("point");
				if(!target_mc.cursorIsOver){
					trace("AAAAAAAAAAAAAAAAROLLOVER SUCCESS AAAAAAAAAAAAAAAAAAAAAAAAAAAAA w: " + collisionRect.width + " _w" + _owidth );
					target_mc.onRollOver();
					target_mc.cursorIsOver = true;
					activeArray.push(target_mc);
				}
				////UPDATE COLLISION XRAY //
				if(collisionRect.width <= _owidth -10){
					with (drawin) {
						if(XRAY){
							beginFill(0x00FF00,50);
							lineStyle(0,0x00FF00,60);
							moveTo(collisionRect.x,collisionRect.y);
							lineTo(collisionRect.x+collisionRect.width,collisionRect.y);
							lineTo(collisionRect.x+collisionRect.width,collisionRect.y+collisionRect.height);
							lineTo(collisionRect.x,collisionRect.y+collisionRect.height);
							lineTo(collisionRect.x,collisionRect.y);
						}
					}
				}

			}else if(target_mc.cursorIsOver){
				trace("BBBBBBBBBBBBBBBBBBBB ROLLOUT BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
				updateUI("open");
			//	target_mc._alpha = 100;
				target_mc.cursorIsOver = false;
				for (var i : String in activeArray) {
					var _mc : MovieClip = MovieClip(activeArray[i]);
					_mc.onRollOut();
					activeArray.splice(Number(i),1);
				}
			}	
			}
		}
		if(hit && XRAY){
		this._alpha = 40;
		}else{
			this._alpha = 100;
		}
		updateAfterEvent(evt);
		}
	}
	public function onMouseDown() : Void{
		trace("onMouseDown");
		if(isToggle){
			isToggled = !isToggled;
			if(isToggled){
				isDown = true;
				updateUI("closed");
			}else{
				isDown = false;
				updateUI("open");
			}
		}else{
			isDown = true;
			updateUI("closed");
		}

		for(var i in activeArray){
			activeArray[i].onPress();
		}
	}
	public function onMouseUp() : Void{
		trace("onMouseUp");
		if(!isToggle){
			isDown = false;
			trace("click " + activeArray.length);
			this.updateUI("open");
		}
		for(var i in activeArray){
			activeArray[i].onRelease();
		}
	}	
	public function updateUI(toState:String):Void{
		if(lockUI){
		}else{
			this.gotoAndStop(toState);
		}
	}
}